package com.xopencode.io.demo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author Mcb
 * @date 2022/5/5
 * @apiNote
 */
public class Client {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        //准备要连接的服务端ip和端口
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 10086);
        if (!socketChannel.connect(inetSocketAddress)){
            while (!socketChannel.finishConnect()){
                System.out.println("Client连接中.....");
            }
        }
        //连接成功之后发消息给服务端
        String message = "Hello Service";
        ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBytes());
        socketChannel.write(byteBuffer);
        System.in.read(); //保持连接
    }
}
